package com.czc.qinglong.util;



import com.fasterxml.jackson.annotation.JsonFormat;

import java.io.Serializable;
import java.util.Date;
import java.util.StringJoiner;


/**
 * 自定义接口返回值
 *
 * @param <T>
 */

public class ResultData<T> implements Serializable {
    private Integer code;
    private String message;
    private T data;
    @JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd HH:mm:ss", timezone = "Asia/Shanghai")
    private Date requestTime;

    public ResultData(Integer code, String message) {
        this.code = code;
        this.message = message;
    }

    public ResultData(Integer code, String message, T data) {
        this(code, message);
        this.data = data;
    }

    public static <T> ResultData<T> success() {
        return new ResultData<>(ResultBackEnum.SUCCESS.getCode(), ResultBackEnum.SUCCESS.getMessage());
    }

    public static <T> ResultData<T> successInfo(String message) {
        return new ResultData<>(ResultBackEnum.SUCCESS.getCode(), message);
    }

    public static <T> ResultData<T> successValueAndInfo(String message,T data) {
        return new ResultData<>(ResultBackEnum.SUCCESS.getCode(), message,data);
    }

    public static <T> ResultData<T> success(T data) {
        return new ResultData<>(ResultBackEnum.SUCCESS.getCode(), ResultBackEnum.SUCCESS.getMessage(), data);
    }

    public static <T> ResultData<T> fail() {
        return new ResultData<>(ResultBackEnum.FAILURE.getCode(), ResultBackEnum.FAILURE.getMessage());
    }

    public static <T> ResultData<T> failInfo(String message) {
        return new ResultData<>(ResultBackEnum.FAILURE.getCode(), message);
    }

    public static <T> ResultData<T> failInfoValueAndInfo(String message,T data) {
        return new ResultData<>(ResultBackEnum.FAILURE.getCode(), message,data);
    }

    public static <T> ResultData<T> fail(T data) {
        return new ResultData<>(ResultBackEnum.FAILURE.getCode(), ResultBackEnum.FAILURE.getMessage(), data);
    }

    public static <T> ResultData<T> error() {
        return new ResultData<>(ResultBackEnum.SERVER_ERROR.getCode(), ResultBackEnum.SERVER_ERROR.getMessage());
    }



/**
     * 自定义错误信息
     *
     * @param message
     * @param <T>
     * @return
     */

    public static <T> ResultData<T> errorInfo(String message) {
        return new ResultData<>(ResultBackEnum.SERVER_ERROR.getCode(), message);
    }

    public static <T> ResultData<T> customError(Integer code, String message, T data) {
        return new ResultData<>(code, message, data);
    }

    public static <T> ResultData<T> customErrorInfo(Integer code, String message) {
        return new ResultData<>(code, message);
    }

    public Integer getCode() {
        return code;
    }

    public void setCode(Integer code) {
        this.code = code;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }

    public Date getRequestTime() {
        return new Date();
    }

    public void setRequestTime(Date requestTime) {
        this.requestTime = requestTime;
    }

    @Override
    public String toString() {
        return (new StringJoiner(", ", ResultData.class.getSimpleName() + "[", "]"))
                .add("code=" + this.code)
                .add("message='" + this.message + "'")
                .add("data=" + this.data)
                .add("requestTime" + this.requestTime)
                .toString();
    }
}

